//
// Created by Dell on 2021/5/31.
//
#include <string>
#include "scene.h"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/ext.hpp"
#include "glm/detail/_noise.hpp"

/**
 * 绘制一个三角形
 * https://learnopengl-cn.readthedocs.io/zh/latest/01%20Getting%20started/04%20Hello%20Triangle/
 */
class Section503 : public Scene {
private:
    GLuint vbo;
    GLuint vertexShader;
    std::string vertexShaderSource = "#version 300 es\n"
                                     "\n"
                                     "layout (location = 0) in vec3 position;\n"
                                     "\n"
                                     "void main()\n"
                                     "{\n"
                                     "    gl_Position = vec4(position.x, position.y, position.z, 1.0);\n"
                                     "}";
    GLuint fragmentShader;
    std::string fragmentShaderSource = "#version 300 es\n"
                                       "precision mediump float;\n"
                                       "out vec4 color;\n"
                                       "\n"
                                       "void main()\n"
                                       "{\n"
                                       "    color = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
                                       "}";
    GLuint shaderProgram;
    GLuint vao;

public:

    void init() override {
        //设置清屏颜色
        glClearColor(0.5f, 0.5f, 0.5f, 1.0f);

        //顶点着色器
        vertexShader = glCreateShader(GL_VERTEX_SHADER);
        const char *a = vertexShaderSource.c_str();
        glShaderSource(vertexShader, 1, &a, nullptr);
        glCompileShader(vertexShader);
        //片段着色器
        fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
        const char *b = fragmentShaderSource.c_str();
        glShaderSource(fragmentShader, 1, &b, nullptr);
        glCompileShader(fragmentShader);
        //创建渲染程序
        shaderProgram = glCreateProgram();
        glAttachShader(shaderProgram, vertexShader);
        glAttachShader(shaderProgram, fragmentShader);
        glLinkProgram(shaderProgram);
        glDeleteShader(vertexShader);
        glDeleteShader(fragmentShader);

        //顶点数据设置
        GLfloat vertices[] = {
                -0.5f, -0.5f, 0.0f,
                0.5f, -0.5f, 0.0f,
                0.0f, 0.5f, 0.0f
        };
        //创建顶点缓冲对象,将顶点数据写入
        glGenBuffers(1, &vbo);
        glBindBuffer(GL_ARRAY_BUFFER, vbo);
        glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
        //创建顶点数组对象
        glGenVertexArrays(1, &vao);
        glBindVertexArray(vao);
        //设置顶点属性指针，0对应顶点着色器layout (location = 0)，每三个值对应一个顶点，float类型，不用规格
        //化，每隔3个float开始读取三个数值作为顶点坐标，数据偏移量
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid *) 0);
        //启用location = 0对应的顶点属性
        glEnableVertexAttribArray(0);
        //解绑vao
        glBindVertexArray(0);
        //解绑vbo
        glBindBuffer(GL_ARRAY_BUFFER, 0);
    }

    void setViewPortSize(float width, float height) override {
    }

    void draw() override {
        // 1. 清除颜色缓冲区，使用指定的ClearColor来刷新背景
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        // 2. 当我们渲染一个物体时要使用着色器程序
        glUseProgram(shaderProgram);
        // 3. 绘制物体
        glBindVertexArray(vao);
        glDrawArrays(GL_TRIANGLES, 0, 3);
        glBindVertexArray(0);
    }

    explicit Section503(AAssetManager *aAssetManager) : Scene(aAssetManager) {

    }

    ~Section503() = default;

};
